package main

import (
	"github.com/scue/go-hello/pipeline"
	"os"
	"fmt"
	"bufio"
)

const (
	SourceFileName = "small.in"
	SourceCount    = 1000000
)

func main() {
	writeSourceFile()
	printSourceInfo()
}

func printSourceInfo() {
	file, err := os.Open(SourceFileName)
	defer file.Close()
	if err != nil {
		panic(err)
	}
	reader := bufio.NewReader(file)
	p := pipeline.ReaderSource(reader, pipeline.ChunkSizeUnlimited)
	for i := 0; i < 100; i++ {
		fmt.Println(<-p)
	}
}

func writeSourceFile() {
	file, err := os.Create(SourceFileName)
	defer file.Close()
	if err != nil {
		panic(err)
	}
	p := pipeline.RandomSource(SourceCount)

	// 使用bufio自带的缓冲区，减少数据频繁的读写
	writer := bufio.NewWriter(file)
	pipeline.WriterSink(writer, p)
	writer.Flush()
}

func mergeDemo() {
	p1 := pipeline.InMemSort(pipeline.ArraySource(3, 2, 3, 6, 8, 0, 2, 1, 4))
	p2 := pipeline.InMemSort(pipeline.ArraySource(10, 6, 7, 2, 1, -1))
	out := pipeline.Merge(p1, p2)
	for v := range out {
		println(v)
	}
}
